package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.hc.bean.GradeBean;
import com.hc.service.CompanyGradeService;
import com.hc.service.GetDataService;
import com.hc.serviceImpl.CompanyGradeServiceImpl;
import com.hc.serviceImpl.GetDataServiceImpl;
import com.hc.util.DateUtil;
import com.hc.util.DateUtilExt;

public class GetDataServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String types = req.getParameter("types");  
		
		
		
//		HttpSession session = req.getSession();  
//		if(session.getAttribute("nameNumber")==null||session.getAttribute("nameNumber").equals("")){
//			return;
//		}
		
		
		
		
		if("rawData".equals(types)){
			
			rawData(req,resp);
			
			
		}else if("gradeData".equals(types)){
			
			grade(req,resp);
			
		}else if("vlanGrade".equals(types)){
			
			vlanGrade(req,resp);
			
		}
		
		
	}
/**
 * Vlan评分
 * @param req
 * @param resp
 */
	private void vlanGrade(HttpServletRequest req, HttpServletResponse resp) {
	
		String startDate = null;
		String stopDate = null;
	

		String vlanGradeDateType = req.getParameter("vlanGradeDateType");  
		String vlanGradeDate =req.getParameter("vlanGradeDate");
		String vlanGradeCompanyText = req.getParameter("vlanGradeCompanyText");  
		String vlanGradeCompanyTextSelect =req.getParameter("vlanGradeCompanyTextSelect");
		
		if(vlanGradeDateType==null||vlanGradeDate==null){
			
			return;
		}
		
		Calendar calendar = DateUtil.str2Calendar(req.getParameter("vlanGradeDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
		
		if("按日".equals(vlanGradeDateType)){
			
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按周".equals(vlanGradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按月".equals(vlanGradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING); 
		}
		
		GetDataService service = new GetDataServiceImpl();
		List list = service.getVlanGradeData(startDate,stopDate,vlanGradeCompanyTextSelect,vlanGradeCompanyText);
		
		String msg = "false";
		if(list!=null){
			JSONArray jsonArray2 = JSONArray.fromObject( list );
			msg = jsonArray2.toString();
		}
		
		
		
		returnMsg(req, resp,msg );
	}
/**
 * 指标评分
 * @param req
 * @param resp
 */
	private void grade(HttpServletRequest req, HttpServletResponse resp) {
		
		String startDate =null;
		String stopDate = null;
		String gradeDateType = req.getParameter("gradeDateType");  
		
		
		String gradeDate =req.getParameter("gradeDate");

		
		if(gradeDate==null||gradeDateType==null){
			
			return;
		}
		
		Calendar calendar = DateUtil.str2Calendar(req.getParameter("gradeDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
		
		if("按日".equals(gradeDateType)){
			
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按周".equals(gradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfWeek(calendar.getTime()), DateUtil.DEFAULT_STRING); 
			
			
		}else if("按月".equals(gradeDateType)){
			
			 startDate = DateUtil.date2Str(DateUtilExt.getFirstDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING);  
			 stopDate = DateUtil.date2Str(DateUtilExt.getLastDayOfMoth(calendar.getTime()), DateUtil.DEFAULT_STRING); 
		}
		
		
		
		
		GetDataService service = new GetDataServiceImpl();
		List<GradeBean> list=	service.getGradeData(startDate,stopDate);
		String msg = "false";
		if(list!=null){
			
			JSONArray jsonArray2 = JSONArray.fromObject( list );
			msg = jsonArray2.toString();
		}
		
		
		
		returnMsg(req, resp,msg );
		
		
	}

	/**
	 * 获取原始数据
	 * @param req
	 * @param resp
	 */
	private void rawData(HttpServletRequest req, HttpServletResponse resp) {
		
			Integer pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
			String selectRawTestType = req.getParameter("selectRawTestType");  
			String companySelect =  req.getParameter("companySelect");  
			String companyText =  req.getParameter("companyText"); 
			if(req.getParameter("rawDataStartDate")==null||req.getParameter("rawDataStopDate")==null){
				
				return ;
			}
			Calendar StartCalendar = DateUtil.str2Calendar(req.getParameter("rawDataStartDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
			Calendar stopCalendar = DateUtil.str2Calendar(req.getParameter("rawDataStopDate")+" 00:00:00", DateUtil.DEFAULT_STRING);
			String startDate = DateUtil.date2Str(DateUtilExt.getFirstTimeOfDay(StartCalendar.getTime()), DateUtil.DEFAULT_STRING);  
			String stopDate = DateUtil.date2Str(DateUtilExt.getLastTimeOfDay(stopCalendar.getTime()), DateUtil.DEFAULT_STRING); 
		
	
			GetDataService service = new GetDataServiceImpl();
			List list = service.getRawData(selectRawTestType, companySelect, startDate , stopDate,pageNumber,companyText);
		
			
			String msg ="";
			
			
			if(list!=null){
				
				JSONArray jsonArray2 = JSONArray.fromObject( list );
				msg =jsonArray2.toString();
				
			}else{
				
				msg = "false";
			}
			
			returnMsg(req,resp,msg);
			
	}
	
	private void returnMsg(HttpServletRequest req, HttpServletResponse resp,String msg) {
		
		resp.setCharacterEncoding("utf-8");
//		resp.setHeader("Content-type", "text/html;charset=UTF-8");
		PrintWriter w;
		try {
			w = resp.getWriter();
			w.write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
